View Javadoc

1   /*
2    * Copyright (C) 1998-2000 Semiotek Inc.  All Rights Reserved.
3    *
4    * Redistribution and use in source and binary forms, with or without
5    * modification, are permitted under the terms of either of the following
6    * Open Source licenses:
7    *
8    * The GNU General Public License, version 2, or any later version, as
9    * published by the Free Software Foundation
10   * (http://www.fsf.org/copyleft/gpl.html);
11   *
12   *  or
13   *
14   * The Semiotek Public License (http://webmacro.org/LICENSE.)
15   *
16   * This software is provided "as is", with NO WARRANTY, not even the
17   * implied warranties of fitness to purpose, or merchantability. You
18   * assume all risks and liabilities associated with its use.
19   *
20   * See www.webmacro.org for more information on the WebMacro project.
21   */
22  
23  
24  package org.webmacro.engine;
25  
26  import org.webmacro.PropertyException;
27  
28  import java.util.HashMap;
29  import java.util.Map;
30  
31  /***
32   * A chained build context, for use in expanding macros.
33   * When called upon to evaluate a reference which is one of the macros
34   * formals, it evaluates the actual argument and returns that, otherwise
35   * it chains back to the build context.
36   */
37  public class MacroBuildContext extends BuildContext
38  {
39  
40      private BuildContext chainedContext, rootContext;
41      private MacroDefinition macro;
42      private Object[] args;
43      private Map macroArgs = new HashMap();
44  
45      public MacroBuildContext (MacroDefinition macro,
46                                Object[] args,
47                                BuildContext bc)
48      {
49          super(bc.getBroker());
50          this.chainedContext = bc;
51          this.args = args;
52          this.macro = macro;
53          String[] argNames = macro.getArgNames();
54          for (int i = 0; i < argNames.length; i++)
55              macroArgs.put(argNames[i], args[i]);
56  
57          rootContext = chainedContext;
58          while (rootContext instanceof MacroBuildContext)
59              rootContext = ((MacroBuildContext) rootContext).chainedContext;
60      }
61  
62      public BuildContext getRootContext ()
63      {
64          return rootContext;
65      }
66  
67      public void putMacro (String name, MacroDefinition macro)
68      {
69          rootContext.putMacro(name, macro);
70      }
71  
72      public MacroDefinition getMacro (String name)
73      {
74          return rootContext.getMacro(name);
75      }
76  
77      public boolean containsKey (Object name)
78      {
79          return macroArgs.containsKey(name) || chainedContext.containsKey(name);
80      }
81  
82      protected Object internalGet (Object name) throws PropertyException
83      {
84          if (macroArgs.containsKey(name))
85              return macroArgs.get(name);
86          else
87          {
88              return rootContext.get(name);
89          }
90      }
91  }
92